package com.lantian.teacher.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantian.common.result.Result;
import com.lantian.common.result.ResultCode;
import com.lantian.teacher.entity.EduTeacher;
import com.lantian.teacher.entity.query.TeacherQuery;
import com.lantian.teacher.exception.EduException;
import com.lantian.teacher.service.EduTeacherService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @name:
 * @description: 讲师 前端控制器
 * @author: meet
 * @create: 2020-04-27 17:52
 */
@CrossOrigin
@RestController
@RequestMapping("/teacher")
@Api(description = "讲师管理")
public class EduTeacherCtrl {

    @Autowired
    private EduTeacherService teacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/list")
    public Result list() {

        try {
            List<EduTeacher> list = teacherService.list(null);
            return Result.ok().data("items", list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("/{id}")
    public Result selectTeacherById(
            @ApiParam(name = "id", value = "要查询的讲师的ID", required = true)
            @PathVariable(value = "id") String id) {

      /*  EduTeacher teacher = teacherService.getById(id);
        if (teacher==null){
            throw new EduException(ResultCode.EDU_ID_ERROR, "没有此讲师信息");
        }*/
        try {
            EduTeacher teacher = teacherService.getById(id);
            return Result.ok().data("teacher", teacher);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation(value = "保存新增讲师")
    @PostMapping("/save")
    public Result saveTeacher(
            @ApiParam(name = "teacher", value = "要保存的teacher对象")
            @RequestBody EduTeacher teacher){
        try {
            teacherService.save(teacher);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }



    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/{id}")
    public Result deleteTeacherById(
            @ApiParam(name = "id", value = "要删除的讲师的ID", required = true)
            @PathVariable(value = "id") String id) {

        try {
            teacherService.removeById(id);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation(value = "修改讲师信息")
    @PutMapping("/update")
    public Result updateTeacherById(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher){
        try {
            teacherService.updateById(teacher);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation(value = "分页查询讲师列表")
  /*  @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页"),
            @ApiImplicitParam(name = "limit", value = "每页显示的记录数")
    })*/
    @GetMapping("/{page}/{limit}")
    public Result selectTeacherByPage(
            @ApiParam(name = "page", value = "当前页")
            @PathVariable Integer page,
            @ApiParam(name = "limit", value = "每页显示的记录数")
            @PathVariable Integer limit){

        try {
            Page<EduTeacher> teacherPage = new Page<>(page, limit);
            teacherService.page(teacherPage,null);
            return Result.ok().data("total", teacherPage.getTotal()).data("rows", teacherPage.getRecords());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation(value = "根据条件 分页查询讲师列表")
    @PostMapping("/{page}/{limit}")
    public Result selectTeacherByPageAndWrapper(
            @ApiParam(name = "page", value = "当前页")
            @PathVariable Integer page,
            @ApiParam(name = "limit", value = "每页显示的记录数")
            @PathVariable Integer limit,
            @ApiParam(name = "query", value = "查询条件")
            @RequestBody TeacherQuery query){

        try {
            Page<EduTeacher> teacherPage = new Page<>(page, limit);
            teacherService.pageQuery(teacherPage, query);
            return Result.ok().data("total", teacherPage.getTotal()).data("rows", teacherPage.getRecords());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
}
